﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.Paint.DefinePaintBrush.CApBrushViewModel
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using EngineeringInternalExtension.DataTypes.Paint;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Forms;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.Paint.DefinePaintBrush;

public class CApBrushViewModel : INotifyPropertyChanged
{
  private CApBrushData _brushData;
  private TxPaintGun _gun;
  private string _dialog_Name;
  private CApPaintBrush _editedBrush;
  private CApPaintBrush _currentBrush;
  private TxPaintBrushManager _mngr;
  private ITxComponent _ghostBrush;
  private TxRobot _robot;

  public CApBrushViewModel()
  {
    this._mngr = TxApplicationEx.PaintManager().GetPaintBrushManager();
    this._robot = ((Collection<ITxObject>) TxApplication.ActiveSelection.GetItems())[0] as TxRobot;
    this._dialog_Name = $"{DnProcessSimulateCommands.Paint.DefinePaintBrush.Resources.StringTable.DEFINE_BRUSH_DIALOG_TITLE} - {((TxBaseGeometryCreationObject) this._robot).Name}";
    if (((Collection<ITxObject>) this._robot.MountedTools).Count > 0)
    {
      foreach (ITxComponent mountedTool in (Collection<ITxObject>) this._robot.MountedTools)
      {
        if (mountedTool is TxPaintGun txPaintGun)
          this._gun = txPaintGun;
      }
    }
    if (this._gun == null)
      this._gun = this.FindAttachmentPaintGun();
    this._brushData = new CApBrushData(this._robot);
    if (this._gun != null)
      return;
    int num = (int) TxMessageBox.Show(DnProcessSimulateCommands.Paint.DefinePaintBrush.Resources.StringTable.GHOST_BRUSH_MISSING_GUN, this._dialog_Name, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  }

  private TxPaintGun FindAttachmentPaintGun()
  {
    TxPaintGun attachmentPaintGun = (TxPaintGun) null;
    foreach (TxPaintGun allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxPaintGun))))
    {
      ITxLocatableObject attachmentParent = allDescendant.AttachmentParent;
      if (attachmentParent != null)
      {
        if (attachmentParent == this._robot)
          attachmentPaintGun = allDescendant;
        else if (TxEngineeringDataInternal.GetScope((ITxObject) attachmentParent) == this._robot)
          attachmentPaintGun = allDescendant;
      }
    }
    return attachmentPaintGun;
  }

  public CApPaintBrush EditedBrush
  {
    get => this._editedBrush;
    set => this._editedBrush = value;
  }

  public CApPaintBrush CurrentBrush
  {
    get => this._currentBrush;
    set => this._currentBrush = value;
  }

  public string DialogName => this._dialog_Name;

  public CApBrushData BrushData
  {
    get => this._brushData;
    set
    {
      this._brushData = value;
      this.NotifyPropertyChanged(nameof (BrushData));
    }
  }

  public bool IsBrushNameExists(string name)
  {
    bool flag = false;
    foreach (CApPaintBrush paintBrushes in (Collection<CApPaintBrush>) this._brushData.PaintBrushesList)
    {
      if (paintBrushes.Name == name && paintBrushes.BrushId != this._editedBrush.BrushId)
      {
        flag = true;
        break;
      }
    }
    return flag;
  }

  public void InitEditedBrush()
  {
    this._editedBrush = new CApPaintBrush();
    if (this._currentBrush == null)
    {
      string str = (this._brushData.PaintBrushesList.Count + 1).ToString();
      this._currentBrush = new CApPaintBrush();
      this._currentBrush.Name = "Brush_" + str;
      this._currentBrush.Robot = this._robot;
    }
    this._editedBrush.BrushId = this._currentBrush.BrushId;
    this._editedBrush.Robot = this._currentBrush.Robot;
    this._editedBrush.Name = this._currentBrush.Name;
    this._editedBrush.OriginFrame = this._currentBrush.OriginFrame;
    this._editedBrush.Solid = this._currentBrush.Solid;
  }

  public bool UpdateEditedBrush()
  {
    bool flag = false;
    if (this._editedBrush != null)
    {
      TxPaintBrushData txPaintBrushData = new TxPaintBrushData();
      txPaintBrushData.OriginFrame = this._editedBrush.OriginFrame;
      txPaintBrushData.Name = this._editedBrush.Name;
      txPaintBrushData.Solid = this._editedBrush.Solid;
      if (this._editedBrush.BrushId == null)
      {
        string str = this._mngr.AddBrush(this._editedBrush.Robot, txPaintBrushData);
        flag = true;
        this._editedBrush.BrushId = str;
        this._brushData.PaintBrushesList.Add(this._editedBrush);
        this.PreserveBrushEntities(this._editedBrush);
        this.NotifyAdded();
      }
      else
      {
        this._mngr.UpdateBrush(this._editedBrush.Robot, this._editedBrush.BrushId, txPaintBrushData);
        flag = true;
        this._currentBrush.Robot = this._editedBrush.Robot;
        this._currentBrush.Name = this._editedBrush.Name;
        this._currentBrush.OriginFrame = this._editedBrush.OriginFrame;
        this._currentBrush.Solid = this._editedBrush.Solid;
        this.PreserveBrushEntities(this._currentBrush);
        this.NotifyEdited();
      }
    }
    return flag;
  }

  public void DeleteBrush()
  {
    if (this._currentBrush == null)
      return;
    CApPaintBrush currentBrush = this._currentBrush;
    this._brushData.PaintBrushesList.Remove(this._currentBrush);
    if (currentBrush.BrushId != null)
      this._mngr.DeleteBrush(currentBrush.Robot, currentBrush.BrushId);
    this.NotifyDeleted();
  }

  public void ClearGhostBrush()
  {
    if (this._ghostBrush != null)
    {
      ((ITxObject) this._ghostBrush).Delete();
      this._ghostBrush = (ITxComponent) null;
    }
    TxApplication.RefreshDisplay();
  }

  public void ShowGhostBrush()
  {
    if (this._ghostBrush != null)
    {
      ((ITxObject) this._ghostBrush).Delete();
      this._ghostBrush = (ITxComponent) null;
      TxApplication.RefreshDisplay();
    }
    if (this.CurrentBrush == null || !TxFrame.op_Inequality(this.CurrentBrush.OriginFrame, (ITxObject) null) || this.CurrentBrush.Solid == null || this._gun == null)
      return;
    try
    {
      this._ghostBrush = this._mngr.CreateGhostBrush(this._currentBrush.Robot, this._gun, this._currentBrush.BrushId);
      if (this._ghostBrush != null)
        ((ITxDisplayableObject) this._ghostBrush).Display();
    }
    catch (TxInvalidObjectException ex)
    {
      int num = (int) TxMessageBox.Show(DnProcessSimulateCommands.Paint.DefinePaintBrush.Resources.StringTable.GHOST_BRUSH_ERROR, this._dialog_Name, MessageBoxButtons.OK, MessageBoxIcon.Hand);
    }
    TxApplication.RefreshDisplay();
  }

  public void PreserveBrushEntities(CApPaintBrush brush)
  {
    ITxDetailedRepresentation originFrame = (ITxDetailedRepresentation) brush.OriginFrame;
    if (originFrame != null && !originFrame.Preserved)
      originFrame.Preserved = true;
    if (!(brush.Solid is ITxDetailedRepresentation solid) || solid.Preserved)
      return;
    solid.Preserved = true;
  }

  public void OnWindowClosing(object sender, CancelEventArgs e) => TxApplication.RefreshDisplay();

  public event PropertyChangedEventHandler PropertyChanged;

  protected void NotifyPropertyChanged(string propertyName)
  {
    if (this.PropertyChanged == null)
      return;
    this.PropertyChanged((object) this, new PropertyChangedEventArgs(propertyName));
  }

  public event CApBrushViewModel.DeletedEventHandler BrushDeleted;

  protected void NotifyDeleted()
  {
    if (this.BrushDeleted == null)
      return;
    this.BrushDeleted((object) this.EditedBrush);
  }

  public event CApBrushViewModel.DeletedEventHandler BrushEdited;

  protected void NotifyEdited()
  {
    if (this.BrushEdited == null)
      return;
    this.BrushEdited((object) this.EditedBrush);
  }

  public event CApBrushViewModel.DeletedEventHandler BrushAdded;

  protected void NotifyAdded()
  {
    if (this.BrushAdded == null)
      return;
    this.BrushAdded((object) this.EditedBrush);
  }

  public delegate void DeletedEventHandler(object sender);

  public delegate void EditedEventHandler(object sender);

  public delegate void AddedEventHandler(object sender);
}
